﻿using System.Text.RegularExpressions;
using System;

namespace curbside.Extensions
{
    public class CheckString : BaseString
    {
        public CheckString(string s) : base(s) { }
    }

    public static class CheckStringExtensions
    {
        public static bool IsInt(this CheckString s, out int result)
        {
            return int.TryParse(s.Value, out result);
        }

        public static bool IsInt(this CheckString s)
        {
            int result;
            return int.TryParse(s.Value, out result);
        }

        public static bool IsDateTime(this CheckString s, out DateTime result)
        {
            return DateTime.TryParse(s.Value, out result);
        }

        public static bool IsDateTime(this CheckString s)
        {
            DateTime result;
            return DateTime.TryParse(s.Value, out result);
        }

        public static bool IsSqlDateTime(this CheckString s, out DateTime result)
        {
            if (IsDateTime(s, out result) && result >= DateTime.Parse("1753/01/01 00:00:00")
                        && result <= DateTime.Parse("9999/12/31 23:59:59.997"))
                return true;

            return false;
        }

        public static bool IsSqlDateTime(this CheckString s)
        {
            DateTime result;

            if (IsDateTime(s, out result) && result >= DateTime.Parse("1753/01/01 00:00:00")
                       && result <= DateTime.Parse("9999/12/31 23:59:59.997"))
                return true;

            return false;
        }

        /// <summary>
        /// 判断指定的字符串是否满足指定的匹配模式
        /// </summary>
        /// <param name="s">任意字符串</param>
        /// <param name="pattern">匹配模式</param>
        /// <returns>如果匹配，返回true； 否则，返回false</returns>
        public static bool IsMatch(this CheckString s, string pattern)
        {
            if (s == null) return false;
            else return Regex.IsMatch(s.Value, pattern);
        }
    }
}
